<?php

/**
 * ErrorController - The default error controller class
 * 
 * @author
 * @version 
 */

class ErrorController extends Zend_Controller_Action
{

    /**
     * This action handles  
     *    - Application errors
     *    - Errors in the controller chain arising from missing 
     *      controller classes and/or action methods
     */
    public function errorAction()
    {
    	$errors = $this->_getParam('error_handler');
    	//载入errorinfo配置
    	$errorConfig = new Zend_Config_Ini(ROOT_PATH . '/application/_config/errorinfo.ini',$errors["exception"]->getCode());
    	$errorInfo = $errorConfig->toArray();
    	//如果gotoUrl为空，默认返回上一页
		if ('' == $errorInfo['gotoUrl']) {
			$errorInfo['gotoUrl'] = $_SERVER['HTTP_REFERER'];
		}
		else {
			$errorInfo['gotoUrl'] = $this->view->host() . $errorInfo['gotoUrl'];
		}
		//判断type
        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                $this->getResponse()->setRawHeader('HTTP/1.1 404 Not Found');
                $this->view->title = 'HTTP/1.1 404 Not Found';
                $this->view->assign($errorInfo);
                break;
            default:
                $this->view->title = 'Application Error';
                $errorInfo['message'] = $errors['exception']->getMessage() . $errorInfo -> message;
                //判断code
                switch ($errors['exception']->getCode()) {
                	case 801:
						$this->view->assign($errorInfo);
                		break;
                	default:
                		$this->view->assign($errorInfo);
                		break;
                }
                break;
        }

		//判断是否生产环境，否则输出完整error信息
		$fc = Zend_Controller_Front::getInstance();
		$this->view->env = $fc->getPlugin('Initializer')->getEnv();
		if ('dev' == $this->view->env || 'test' == $this->view->env) {
			$this->view->ErrorMessage = $errors->exception;
			var_dump($errors);
		}
	}
}
